/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.DAO;


import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import transervfx.Dato.Entidad.Localidad;
import transervfx.Dato.Entidad.ZonaRegional;

/**
 *
 * @author oscar
 */
public class ZonaRegionalDAO implements Dao<ZonaRegional,Long> {
   
    private static ZonaRegionalDAO INSTANCE = null;
    
    
     /**
     *  Crea el Gestor Auxiliar
     */
    protected ZonaRegionalDAO() {
    }

    /**
     * creador sincronizado para protegerse de posibles problemas  multi-hilo
     * otra prueba para evitar instanciación múltiple
     */
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ZonaRegionalDAO();
        }
    }

    /**
     * Obtiene la instancia.
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static ZonaRegionalDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }
    

    @Override
    public void persist(ZonaRegional entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                em.persist(entity);
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    @Override
     //Sin transaccion
    public void remove(ZonaRegional entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            em.remove(entity);

        } finally {
            em.close();
        }
    }
    
    @Override
     public void  update(ZonaRegional bean){
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

          em.getTransaction().begin();
          if(em.contains(bean))
          {
            em.merge(bean);
          }
            em.getTransaction().commit();

        } finally {
            em.close();
        }
       
        
    }

    @Override
    public ZonaRegional findById(Long id) {
       EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(ZonaRegional.class, id);

        } finally {
            em.close();
        }
    }
    

  
    @Override
    public List<ZonaRegional> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("ZonaRegional.findAll").getResultList();

        } finally {
            em.close();
        }
    }

   public ZonaRegional findByNombre(String nombre,Localidad l) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            Query query = em.createNamedQuery("ZonaRegional.findByNombre");
            query.setParameter("nombre", nombre);
            query.setParameter("nombreLocalidad", l.getNombre());
           if(query.getResultList().isEmpty()){
                return null;
            }
            return (ZonaRegional) query.getResultList().get(0);
            
            
        } finally {
            em.close();
        }
    }

    public boolean existeZona(ZonaRegional z) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
 

}
